package sxe.core.wifi;

import snobject.snObject;
import snobject.snObjectArgList;
import snobject.snString;
import snobject.snStruct;
import step.EvaluationFailure;
import step.FunctionWrapper;
import step.exceptions.ResourceUnavailableException;

public class Strongest extends FunctionWrapper {


	/* takes a bunch of snWifiStructs and returns the one with best
	 * signal strength (RSSI field) */

	@Override
	public snObject Call(snObjectArgList argv) throws EvaluationFailure,
			ClassCastException, ArrayIndexOutOfBoundsException,
			ResourceUnavailableException {

		
		snStruct bestS = argv.popStruct();
		int bestSig = Integer.valueOf(((snString)bestS.get("RSSI")).getString());
		int tempSig;
		
		snStruct tempS = null;
		
		/* takes lots of signal packets and returns the strongest */
		while(argv.hasNext()){
			tempS = argv.popStruct();
			tempSig = Integer.valueOf(((snString)tempS.get("RSSI")).getString());
			if((tempSig!=0)&&(tempSig < bestSig)){
				bestSig = tempSig;
				bestS = tempS;
			}
		}
		
		return bestS;
	}

}
